﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Globalization;
using System.Diagnostics;

namespace RStein.PosterousReader.WP.Extensions
{
  public static class StringExtensions
  {

    public static readonly string CHAR_UNICODE_FORMAT = "&#{0};";
    public static readonly int ASCII_INDEX = 127;    

    //Dirty fix for WP7 browser
    public static string EncodeUnicodeChars(this string srcString)
    {
      if (srcString == null)
      {
        throw new ArgumentNullException("srcString");
      }

      var resultString = srcString.Aggregate(new StringBuilder(),
                                  (sb, ch) =>
                                    {
                                      var intCh = (int) ch;
                                      if (intCh > ASCII_INDEX)
                                      {                                        
                                        sb.AppendFormat(CHAR_UNICODE_FORMAT, intCh);
                                      }                                    
                                      else
                                      {
                                        sb.Append(ch);
                                      }
                                      return sb;
                                    },
                                      sb => sb.ToString()
                                  );
      return resultString;

    }


    public static string HtmlDecode(this string srcString)
    {
      if (srcString == null)
      {
        throw new ArgumentNullException("srcString");
      }
      var retString  = HttpUtility.HtmlDecode(srcString);
      Debug.WriteLine(retString);
      return retString;
    }
  }
}
